﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the Answers section of the database.
    // The middle layer of the DAL.
    class SqlApplicantsProgressRepository : IApplicantsProgressRepository
    {
        private Table<ApplicantProgress> applicantsProgressTable;

        public SqlApplicantsProgressRepository(string connectionString)
        {
            applicantsProgressTable = (new DataContext(connectionString)).GetTable<ApplicantProgress>();
        }

        public IQueryable<ApplicantProgress> ApplicantsProgress
        {
            get { return applicantsProgressTable; }
        }

        public void SaveApplicantProgress(ApplicantProgress applicantProgress)
        {
            if (applicantProgress.ApplicantProgressID == 0)
            {
                applicantsProgressTable.InsertOnSubmit(applicantProgress);
            }
            else
            {
                applicantsProgressTable.Attach(applicantProgress);
                applicantsProgressTable.Context.Refresh(RefreshMode.KeepCurrentValues, applicantProgress);
            }

            applicantsProgressTable.Context.SubmitChanges();
        }

        public void DeleteApplicantProgress(ApplicantProgress applicantProgress)
        {
            applicantsProgressTable.DeleteOnSubmit(applicantProgress);
            applicantsProgressTable.Context.SubmitChanges();
        }

        public bool Save()
        {
            try
            {
                applicantsProgressTable.Context.SubmitChanges();
            }
            catch (Exception)
            {
                //this.SetError(ex);
                return false;

            }
            return true;

        }
    }
}